<?
include 'order_head.php';
?>
<style type="text/css">
	select{
		width:50px;
		padding:3px;
	}
	
</style>
<h2>จัดส่งสินค้า</h2>
<table cellpadding="6" cellspacing="1"  border="0" class="myTable table2">
<form method="post" action="<?php echo base_url()?>order/summaryDI/">
<tr>
  <th style="width:5%">&nbsp;</th>
  <th style="width:45%">สินค้า</th>
  <th style="text-align:center;width:10%">จำนวน</th>
  
  <th style="text-align:center;width:40%">จำนวนส่ง</th>
  
  
</tr>

<?php $i = 1; ?>
<?
$allDiscount = 0;
$totalSale = 0;
$totalPremium = 0;
?>
<?php foreach ($this->cart->contents() as $items): ?>

	<?
	
	$option = $this->cart->product_options($items['rowid']);
	
	$option = (array)$option;
	$pID = $option['MATNR'];
	$pName = $option['MAKTX'];
	$baseUnit = $option['BASE_UNIT'];
	if($baseUnit=="KAR"){
		$b = "ลัง";
	}else if($baseUnit=="PAK"){
		$b = "แพ็ก";
	}else {
		$b = "ชิ้น";
	}
	?>
	<?
	$class="";
	$itemType = "sale";
	if(isset($option['ISPremium'])){
		if($option['ISPremium']=="Yes"){
			$class = "line-preimum";
			$totalPremium+=$items['subtotal'];
			$pName = $pName."<br>(สินค้าแถม)";
			$itemType = "premium";	
		}
		
	}else{
		$totalSale+=$items['subtotal'];
	}
	//print_r($items);exit();
	?>
	<tr class="<?php echo $class?>">
		<td><input type="checkbox" checked="checked" class="isSend">
			<input type="hidden" name="pID[]"  value="<?php echo $items['id']?>" />
			<input type="hidden" name="itemType[]"  value="<?php echo $itemType ?>" />
			
		</td>
	  <td style="width:40%"><?php echo $pID ?><br />
		<?php echo $pName ?></td>
	
		<td style="width:10%;text-align: right;"><?php echo converUnit($items,$option);?></td>
		 
		
	
		<td style="text-align:right;width:60px">
	  	<div style="width:100%;float:right;">
			<?php echo converUnitCombo($items,$option)?>
	  		</div>
	  </td> 
	 
	  
	  
	  
	  
	</tr>

<?php $i++; ?>

<?php endforeach; ?>

<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>ราคารวม</strong></td>
  
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($totalSale); ?></td>
</tr>
<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>ราคาของแถม</strong></td>
  
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($totalPremium); ?></td>
</tr>
<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>ส่วนลด</strong></td>
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($discount); ?></td>
</tr>
<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>สุทธิ</strong></td>
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($totalSale - $discount); ?></td>
</tr>
</table>
<br /> 
<a class="btn-default" href="<?php echo base_url()?>order/addpromotion/"><i class="fa fa-chevron-left"></i> แก้ไข promotion</a>
<button type="submit" class="btn-next"><i class="fa fa-chevron-right"></i> ข้อมูลชำระเงิน</button>
</form>
<script type="text/javascript">
	$(function(){
		
		$('.splitCheck').click(function(){
		
			var start = $(this).is(":checked");
			var p = $(this).parents('td');
			var obj = p.find('.split')
			if(start){
				obj.show();
				p.find('select').find('option:first').attr("selected","selected");
				p.find('input[type="hidden"]').val("0")
			}else{
				obj.hide();
				p.find('input[type="hidden"]').val($(this).val());
			}
		})
		$('select').change(function(){
			var p = $(this).parents('td');
			var num = 0;
			$.each(p.find('select'),function(){
				num+=$(this).val() * parseInt($(this).attr("ratio"));
			})
			p.find('input[type="hidden"]').val(num);
		});
		$('.isSend').click(function(){
			var start = $(this).is(":checked");
			var p = $(this).parents('tr');
			var input = p.find('input[type="hidden"][name="sendQty[]"]');
			if(start){
				input.val(input.attr("default-qty"));
				p.find('td:last').find('div').show();
			}else{
				input.val(0);
				p.find('td:last').find('div').hide();
			}
		})
	})
</script>
